using System;
using System.Web;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using RealDolmen.Helpers;

namespace RealDolmen.SharePoint.Controls
{
    public class FixedLayoutsPageBase : LayoutsPageBase
    {
        #region Fields
        private string processedUri = string.Empty; 
        #endregion // Fields

        #region Properties
        private SiteMapNode currentNode = null;

        public SiteMapNode CurrentNode
        {
            get { return currentNode; }
            set { currentNode = value; }
        }

        protected SiteMapProvider Provider
        {
            get { return ((CurrentNode == null) ? SiteMap.Providers["SPXmlContentMapProvider"] : CurrentNode.Provider); }
        }
        #endregion // Properties

        #region Events
        protected override void CreateChildControls()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                processedUri = HttpContext.Current.Request.RawUrl;
                Provider.SiteMapResolve +=
                   new SiteMapResolveEventHandler(GenerateSiteMapPath);
            });
            base.CreateChildControls();
        }

        /*
         * GETS CALLED TWO TIMES WITH TWO FRONT ENDS.. WHY???
         */
        private SiteMapNode GenerateSiteMapPath(object sender, SiteMapResolveEventArgs e)
        {
            string currentUri = e.Context.Request.RawUrl;

            if (currentUri == processedUri)
            {
                return CurrentNode;
            }
            return null;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (Provider is XmlSiteMapProvider)
                {
                    XmlSiteMapProvider xmlSiteMapProvider = Provider as XmlSiteMapProvider;
                    WrapperObject xmlSiteMapProviderWrapper = new WrapperObject(xmlSiteMapProvider);

                    xmlSiteMapProviderWrapper.Invoke("Clear");

                    xmlSiteMapProvider.BuildSiteMap();
                }
                Provider.SiteMapResolve -= new SiteMapResolveEventHandler(GenerateSiteMapPath);
            });
        }
        #endregion // Events

        #region Nodes
        protected void FindNode(string key)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                AddNode(Provider.FindSiteMapNode(key));
            });
        }

        protected void FindNode(string key, string url)
        {
            FindNode(key);
            CurrentNode.ReadOnly = false;
            CurrentNode.Url = url;
        }

        protected void AddNode(string title, string url)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                AddNode(new SiteMapNode(Provider, url, url, title));
            });
        }

        private void AddNode(SiteMapNode node)
        {
            if (CurrentNode != null)
            {
                node.ParentNode = CurrentNode;
            }

            CurrentNode = node;
        }
        #endregion // Nodes
    }
}
